#include "matrixstack/matrixstack.h"

#include <iostream>

#include "matrixstack/offset.h"

MatrixStack* guiStack;

void MatrixStack::Reset(){
    offsets.clear();
//    std::cout<<"\nStack reset\n";
}

void MatrixStack::PushTransform(){
    offsets.push_back(Offset());
}

void MatrixStack::PopTransform(){
    if(offsets.size()==0){
        std::cout<<"WARNING: Stack underflow\n";
        return;
    }
    offsets.pop_back();
//    std::cout<<"Stack popped, total x: "<<Getx()<<std::endl;
}

void MatrixStack::Translate(int x, int y, int z){
    GetTopTranslation()+=Offset(x, y, z);
//    std::cout<<"Pushing x: "<<x<<std::endl;
//    std::cout<<"Total x: "<<Getx()<<std::endl;
}

Offset& MatrixStack::GetTopTranslation(){
    if(offsets.size() == 0){
        std::cout<<"WARNING: Matrix stack empty, appending empty entry\n";
        offsets.push_back(Offset());
    }
    return offsets.back();
}

int MatrixStack::Getx(){
    int x = 0;
    for(auto off:offsets){
        x+=off.Getx();
    }
    return x;
}

int MatrixStack::Gety(){
    int y = 0;
    for(auto off:offsets){
        y+=off.Gety();
    }
    return y;
}
int MatrixStack::Getz(){
    int z = 0;
    for(auto off:offsets){
        z+=off.Getz();
    }
    return z;
}
